package net.net23.sysdent.client.ui.menu;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;

public class MenuItem extends Composite {

	private String key;
	private String text;

	private Anchor a;
	private MenuItemHandler menuItemHandler;
	
	private String mainClass ="t4l-menu-item";

	public MenuItem(String key) {
		this(key, key);
	}
	
	public void setMainClass(String mainClass) {
		this.mainClass = mainClass;
		a.setStyleName(getMainClass());
	}
	
	public String getMainClass() {
		return mainClass;
	}

	public MenuItem(String text, String key) {
		this.text = text;
		this.key = key;
		a = new Anchor(text);
		a.setStyleName(getMainClass());
		a.getElement().setId(this.key);
		sinkEvents(Event.ONMOUSEOVER | Event.ONMOUSEOUT | Event.ONCLICK);
		initWidget(a);
	}

	public void onBrowserEvent(Event event) {
		switch (DOM.eventGetType(event)) {
		case Event.ONMOUSEOVER: {
			a.removeStyleName(getMainClass()+"-plain");
			a.addStyleName(getMainClass()+"-over");
			break;
		}
		case Event.ONMOUSEOUT: {
			a.removeStyleName(getMainClass()+"-over");
			a.addStyleName(getMainClass()+"-plain");
			break;
		}
		case Event.ONCLICK: {
			if (menuItemHandler != null) {
				menuItemHandler.itemSelected(this);
			}
			break;
		}
		}
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public void addMenuItemHandler(MenuItemHandler menuItemHandler) {
		this.menuItemHandler = menuItemHandler;
	}
}
